package com.ucs.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import com.ucs.domain.Category;
import com.ucs.domain.Order;
import com.ucs.domain.User;

import android.util.Log;

public class UCSUtil {
	private static final String TAG="ucs";	
	
	@SuppressWarnings("unchecked")
	public static String downloadCategories(){
		HttpClient httpClient = getHttpClient();
		try{
			HttpGet httpGet = new HttpGet(getCategoryURL());
			HttpResponse httpResponse = httpClient.execute(httpGet);
			String xml = EntityUtils.toString(httpResponse.getEntity());
			List<Category> categories = (List<Category>)XStremUtil.toJava(xml);
			UCSData.getInstance().setCategories(categories, xml);
			return "Categories downloaded";
		}
		catch (Exception e) {
			Log.e(TAG, "Could not getText", e);
			return "Categories could not be downloaded: "+e.getMessage() ;
		}
	}
	@SuppressWarnings("unchecked")
	public static String downloadUsers(){
		HttpClient httpClient = getHttpClient();
		try{
			HttpGet httpGet = new HttpGet(getUserURL());
			HttpResponse httpResponse = httpClient.execute(httpGet);
			String xml = EntityUtils.toString(httpResponse.getEntity());
			List<User> users = (List<User>)XStremUtil.toJava(xml);
			UCSData.getInstance().setUsers(users, xml);
			return "Users downloaded";
		}
		catch (Exception e) {
			Log.e(TAG, "Could not getText", e);
			return "Users could not be downloaded: "+e.getMessage() ;
		}
	}
	
	public static boolean postOrder(Order order){
		HttpClient httpClient = getHttpClient();
		
		try {
			Log.d("ucs", "+UCSUtil:postOrder");
			HttpPost httpPost = new HttpPost(getOrderURL());
			StringEntity stringEntity = new StringEntity(XStremUtil.toXML(order));
			httpPost.setEntity(stringEntity);
			HttpResponse httpResponse = httpClient.execute(httpPost);
			String orderXML = EntityUtils.toString(httpResponse.getEntity());
			Log.d("ucs", "orderXML="+orderXML);
			if ("-1".equals(orderXML)){
				return false;
			}
			order = (Order)XStremUtil.toJava(orderXML);
			Log.d("ucs", "-UCSUtil:postOrder");
			return true;
		} catch (UnsupportedEncodingException e) {
			Log.e("ucs", "Could not create StringEntity", e);
			return false;
		} catch (ClientProtocolException e) {
			Log.e("ucs", "Client Protocol Exception", e);
			return false;
		} catch (IOException e) {
			Log.e("ucs", "IO Exception", e);
			return false;
		} catch (Throwable t){
			Log.e("ucs", "generic Exception", t);			
			return false;
		}
	}
	
	@SuppressWarnings("unchecked")
	public static List<Order> getOpenOrderList(){
		HttpClient httpClient = getHttpClient();		
		try {
			HttpGet httpGet = new HttpGet(getOrderURL()+"/"+UCSData.getInstance().getLoggedInUser().getUserID());
			HttpResponse httpResponse = httpClient.execute(httpGet);
			String ordersXML = EntityUtils.toString(httpResponse.getEntity());
//			Log.d("ucs", ordersXML);
			return (List<Order>)XStremUtil.toJava(ordersXML);
		} catch (UnsupportedEncodingException e) {
			Log.e("ucs", "Could not create StringEntity", e);
			return null;
		} catch (ClientProtocolException e) {
			Log.e("ucs", "Client Protocol Exception", e);
			return null;
		} catch (IOException e) {
			Log.e("ucs", "IO Exception", e);
			return null;
		} catch (Throwable t){
			Log.e("ucs", "generic Exception", t);			
			return null;
		}
	}
	
	private static HttpClient getHttpClient() {
		HttpClient httpClient = new DefaultHttpClient();
		HttpParams httpParams = httpClient.getParams();
		httpParams.setIntParameter("http.socket.timeout", 50000);
		httpParams.setIntParameter("http.connection.timeout", 500);
		return httpClient;
	}
	private static String getCategoryURL() {
		return getServerURL() + "categories";
	}
	private static String getUserURL() {
		return getServerURL() + "users";
	}
	private static String getServerURL() {
		String serverURL = "http://"+UCSData.getInstance().getServername()+":8182/";
		return serverURL;
	}
	private static String getOrderURL() {
		return getServerURL()+"orders";
	}
	
	public static void setServername(String servername) {
		UCSData.getInstance().setServername(servername);
	}
	public static String getServername() {
		return UCSData.getInstance().getServername();
	}
}